home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-06-16 | 12.1 KB | 394 lines | [TEXT/MMCC] |
- //
- // Application: ShowHideDemo
- //
- // Description: This demonstrates how to hide the menubar and/or
- // the desktop. The desktop is hidden simply by creating
- // a large background window and filling it with the
- // appropriate pattern. The menu bar is hidden by zeroing
- // the Menu bar height, updating the GrayRgn, and then
- // forcing all windows to redraw. The rounded corners on
- // every screen device can also be removed using a method
- // similar to that used with the menubar.
- //
- // Programmer: David Hayward
- // Developer Technical Support
- // Apple Computer, Inc.
- //
- // Environment: Metrowerks C version 6, Universal Interfaces 2.0
- //
- // History: 12/10/93
- // first draft
- // 5/12/95
- // updated project for Metrowerks
- //
-
-
- #include <Desk.h>
- #include <Events.h>
- #include <Dialogs.h>
- #include <LowMem.h>
- #include <Memory.h>
- #include <Windows.h>
- #include <QuickDraw.h>
- #include <ToolUtils.h>
-
- #include "InitMac.h"
- #include "ShowHideMenubar.h"
- #include "ShowHideCorners.h"
-
-
- /**\
- |**| ==============================================================================
- |**| ENUMS
- |**| ==============================================================================
- \**/
- enum { kForeWind=128, kBackWind } ;
- enum { kMBarID=128 } ;
- enum { kAppleMenu=128, kFileMenu, kEditMenu, kShowHideMenu } ;
- enum { kNewItem=1, kOpenItem, kCloseItem, kSaveItem,
- kSaveAsItem, kFileBlank1, kPageSetupItem,
- kPrintItem, kFileBlank2, kQuitItem } ;
- enum { kShowHideMBar=1, kShowHideBack, kShowHideFore, kShowHideCorners } ;
-
-
- /**\
- |**| ==============================================================================
- |**| GLOBALS
- |**| ==============================================================================
- \**/
- WindowPtr gForeWind; /* the window pointers */
- WindowPtr gBackWind;
- Boolean gMBarVis = true; /* hold the current state */
- Boolean gForeVis = true;
- Boolean gBackVis = true;
- Boolean gCornVis = true;
- Boolean gDone = false;
- MenuHandle AppleMenuHdl, /* the menu handles */
- FileMenuHdl,
- EditMenuHdl,
- LabelMenuHdl,
- SpecialMenuHdl;
-
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE FUNCTION PROTOTYPES
- |**| ==============================================================================
- \**/
- void main ( void ) ;
- void SetUpMenus ( void ) ;
- void CreateWindows ( void ) ;
- void DoCommand ( long mResult ) ;
- void DoUpdate ( WindowPtr whichWindow ) ;
- void DoMouseDown ( EventRecord event ) ;
- void DoOSEvent ( EventRecord event ) ;
- void DoEventLoop ( void );
-
-
- /**\
- |**| ==============================================================================
- |**| PRIVATE FUNCTIONS
- |**| ==============================================================================
- \**/
-
-
- /*------------------------------------------------------------------------------*\
- main
- *------------------------------------------------------------------------------*
- a fairly normal main function.
- \*------------------------------------------------------------------------------*/
- void main ( void )
- {
- InitToolBox( 4 ) ;
- SetUpMenus() ;
- CreateWindows() ; /* create fore and back windows */
- DoEventLoop() ;
- SetMBarState( SHOW ) ; /* be sure to show mbar before exiting */
- SetCornersState( SHOW ) ; /* be sure to show corners before exiting */
- }
-
-
- /*------------------------------------------------------------------------------*\
- SetUpMenus
- *------------------------------------------------------------------------------*
- load in and draw the menu bar.
- \*------------------------------------------------------------------------------*/
- void SetUpMenus ( void )
- {
- Handle hMenuBar ;
-
- hMenuBar = GetNewMBar( kMBarID ) ;
- SetMenuBar( hMenuBar ) ;
-
- AppleMenuHdl = GetMHandle( kAppleMenu ) ;
- FileMenuHdl = GetMHandle( kFileMenu ) ;
- EditMenuHdl = GetMHandle( kEditMenu ) ;
- SpecialMenuHdl = GetMHandle( kShowHideMenu ) ;
-
- AddResMenu( AppleMenuHdl, 'DRVR' ) ;
-
- DrawMenuBar() ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- CreateWindows
- *------------------------------------------------------------------------------*
- Create a normal window in front and a big
- window in back to obscure the Finder.
- \*------------------------------------------------------------------------------*/
- void CreateWindows ( void )
- {
- Rect r;
- RgnHandle GrayRgn = LMGetGrayRgn() ;
-
- gForeWind = GetNewCWindow( kForeWind, nil, nil ) ;
- gBackWind = GetNewCWindow( kBackWind, nil, nil ) ;
-
- /* resize the back window to cover the entire srceen. */
- /* For debugging purposes I'm only making it as big as the */
- /* main sceen. A real app would probably make it as big as */
- /* necessrary to cover all monitors. One way to get this */
- /* rect is to Union the rect of the main screen device (i.e. */
- /* screenBits.bounds - which includes the menu bar) with the */
- /* bounds of the entire GrayRgn ((**GrayRgn).rgnBBox - which */
- /* may not enclude the menu bar) */
-
- // r = qd.screenBits.bounds; /* main screen only - for debugging */
-
- UnionRect( &((**GrayRgn).rgnBBox), &(qd.screenBits.bounds), &r ) ;
-
- MoveWindow( gBackWind, r.left, r.top, false ) ;
- SizeWindow( gBackWind, r.right-r.left, r.bottom-r.top, true ) ;
-
- SelectWindow( gForeWind ) ; /* bring fore wind to front */
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoCommand
- *------------------------------------------------------------------------------*
- handle menu events.
- \*------------------------------------------------------------------------------*/
- void DoCommand ( long mResult )
- {
- short item, menu, mark ;
- MenuHandle mHandle ;
-
- item = LoWord( mResult ) ;
- menu = HiWord( mResult ) ;
- mHandle = GetMHandle( menu ) ;
-
- switch (menu)
- {
- case kShowHideMenu : /* if the the ShowHide menu... */
- switch ( item )
- {
- case kShowHideMBar : /* if MBar item... */
- SetMBarState( !GetMBarState() ) ;
- gMBarVis = !gMBarVis ;
- DoUpdate( gBackWind ) ;
- break ;
- case kShowHideBack : /* if Back Window item... */
- if ( gBackVis )
- HideWindow( gBackWind ) ; /* show or hide the window */
- else
- ShowWindow( gBackWind ) ;
- gBackVis = !gBackVis ; /* toggle the state global */
- break ;
- case kShowHideFore : /* if Fore Window item... */
- if ( gForeVis )
- HideWindow( gForeWind ) ; /* show or hide the window */
- else
- ShowWindow( gForeWind ) ;
- gForeVis = !gForeVis; /* toggle the state global */
- SelectWindow( gForeWind ) ; /* bring fore wind to front */
- break ;
- case kShowHideCorners : /* if corners item... */
- SetCornersState( !GetCornersState() ) ;
- gCornVis = !gCornVis ;
- DoUpdate( gBackWind ) ;
- break ;
- }
- break;
-
- case kFileMenu : /* if the the file menu... */
- if ( item==kQuitItem ) /* if quit item... */
- gDone = true ;
- break ;
- }
- HiliteMenu(0); /* un-hilight the chosen menu item */
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoUpdate
- *------------------------------------------------------------------------------*
- handle update events.
- \*------------------------------------------------------------------------------*/
- void DoUpdate ( WindowPtr window )
- {
- Str255 str;
-
- BeginUpdate( window ) ;
- SetPort( window ) ; /* make sure we're in our port */
-
- if ( window==gBackWind ) /* if updating the back window */
- {
- Rect r;
-
- /* we want to set the origin of the window to be the origin */
- /* of the global coordinate system so that the pattern we */
- /* draw is not offset from the desktop's pattern */
-
- SetOrigin( ((GrafPtr)window)->portRect.left,
- ((GrafPtr)window)->portRect.top ) ;
-
- r = ((GrafPtr)window)->portRect ;
-
- /* for this example we'll fill the back window with same pattern */
- /* as the finder's desktop but you may want to fill it else */
-
- if ( LMGetSPMisc2() & 0x80 ) /* if bit 7 of this lowmem global is set then */
- { /* a color PixPattern is the background pat */
- FillCRect( &r, GetPixPat(16) ) ; /* so use the 'ppat'=16 resource in system */
- }
- else /* otherwise desktop is an old-style b&w pattern */
- { /* in the Low Memory globals */
- Pattern patt ;
- LMGetDeskPattern( &patt ) ; /* so get the B&W pattern from LowMem */
- FillRect( &r, (ConstPatternParam)&patt ) ; /* and fill the window with the pattern */
- }
- SetOrigin( 0, 0 ) ; /* restore the origin */
- }
- if (window == gForeWind) /* if updating the fore window */
- {
- MoveTo( 50, 50 ) ;
- DrawString( "\pHello World" ) ;
- }
-
- EndUpdate( window ) ;
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoMouseDown
- *------------------------------------------------------------------------------*
- handle DoMouseDown events.
- \*------------------------------------------------------------------------------*/
- void DoMouseDown ( EventRecord event )
- {
- WindowPtr window ;
- short clickArea ;
- Rect screenRect ;
-
- clickArea = FindWindow( event.where, &window ) ;
- screenRect = (**GetGrayRgn()).rgnBBox ;
-
- switch ( clickArea )
- {
- case inSysWindow:
- SystemClick( &event, window ) ;
- break;
-
- case inMenuBar: /* handle menu selections */
- DoCommand( MenuSelect(event.where) ) ;
- break;
-
- case inDrag : /* handle window drags */
- DragWindow( window, event.where, &screenRect ) ;
- break;
-
- case inContent : /* Handle content clicks */
- if ( window != FrontWindow() ) /* if click is in a back window */
- if ( window != gBackWind ) /* and its not gBackWind */
- SelectWindow( window ); /* the bring it to front */
- break;
-
- case inGoAway :
- if ( TrackGoAway(window,event.where) )
- HideWindow( window ) ; /* hide the window */
- if ( window==gForeWind ) /* if it was the fore wind */
- gForeVis = false ; /* then update its state global */
- break;
- }
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoOSEvent
- *------------------------------------------------------------------------------*
- handle DoOSEvent events.
- \*------------------------------------------------------------------------------*/
- void DoOSEvent ( EventRecord event )
- {
- long mssg ;
-
- mssg = event.message ;
- if ( (mssg>>24)==suspendResumeMessage ) /* if high byte of message indicates */
- { /* this is suspend/resume event */
- if ( mssg & resumeFlag ) /* if resume event */
- {
- /* we're switching back from another app so we need to show */
- /* the menubar, the rounded corners, and the background window */
- /* if the globals (gMBarVis, gCornVis, gBackVis) say we should */
-
- if (gBackVis) ShowWindow(gBackWind) ;
- if (!gCornVis) SetCornersState(HIDE) ;
- if (!gMBarVis) SetMBarState(HIDE) ;
- }
- else /* if suspend event */
- {
- /* we're switching to another app so be sure to show the menubar */
- /* and the rounded corners and hide the background window */
- /* if the globals (gMBarVis, gCornVis, gBackVis) say we should */
-
- if (gBackVis) HideWindow(gBackWind) ;
- if (!gCornVis) SetCornersState(SHOW) ;
- if (!gMBarVis) SetMBarState(SHOW) ;
- }
- }
- }
-
-
- /*------------------------------------------------------------------------------*\
- DoEventLoop
- *------------------------------------------------------------------------------*
- Keep doing an event loop until the user quits.
- \*------------------------------------------------------------------------------*/
- void DoEventLoop ( void )
- {
- EventRecord event ;
- WindowPtr window ;
- long mssg ;
-
- while ( !gDone )
- {
- if ( WaitNextEvent(everyEvent,&event,30,nil) )
- {
- mssg = event.message ;
- switch ( event.what )
- {
- case mouseDown : /* handle mouse clicks */
- DoMouseDown( event ) ;
- break ;
-
- case keyDown : /* handle key hits */
- case autoKey :
- if ( event.modifiers & cmdKey ) /* handle command keys */
- DoCommand(MenuKey( mssg & charCodeMask) ) ;
- break ;
-
- case updateEvt : /* handle update events */
- DoUpdate( (WindowPtr)mssg ) ;
- break ;
-
- case osEvt: /* handle os events */
- DoOSEvent( event ) ;
- break ;
- }
- }
- }
- }
-
-